﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;

namespace WebApplication1
{
    public partial class viewMinutesById : System.Web.UI.Page
    {
        string fileName;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string minuteId = Request.QueryString.ToString();
                MemberAL memberAL = new MemberAL();
                List<MinutesDTO> minutes = memberAL.GetMinutesById(new Guid(minuteId)).ToList();
                IList<MinutesDTO> minuteIList = minutes;
                dvMinutesById.DataSource = minuteIList;
                dvMinutesById.DataBind();

                foreach (MinutesDTO mint in minutes)
                {
                    if (mint.FilePath == "")
                    {
                    }
                    else
                    {
                        lblMinuteBydId.Text =  "Open / Download "+ mint.Type;
                        fileName = mint.FilePath;
                        lnkMinutes.Text = mint.FilePath;
                        //lnkMinutes.Text = "";
                    }
                }
            }
            
        }

        protected void lnkMinutes_Click(object sender, EventArgs e)
        {
            
            
            string fName = lnkMinutes.Text;
            //string fname = fileName
            string FilePath = MapPath("minutes");
            FileInfo fi = new FileInfo(FilePath +"\\" + fName);
            long sz = fi.Length;

            Response.ClearContent();
            Response.ContentType = MimeType(Path.GetExtension(fName));
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename = {0}", System.IO.Path.GetFileName(fName)));
            Response.AddHeader("Content-Length", sz.ToString("F0"));
            Response.TransmitFile(FilePath + "\\" + fName);
            Response.End();
        }

        
        public static string MimeType(string Extension)
        {
            string mime = "application/octetstream";
                if (string.IsNullOrEmpty(Extension))
                    return mime;
 
                    string ext = Extension.ToLower();
                    Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
                if (rk != null && rk.GetValue("Content Type") != null)
                    mime = rk.GetValue("Content Type").ToString();
                return mime;
    } 
  
        private void DownloadFile(string fname)
        {
            string path = MapPath("minutes\\"+fname);
            //string name = Path.GetFileName(path);
            //string ext = Path.GetExtension(path);
            string ext = "";
            string type = "";
            // set known types based on file extension  
            if (ext != null)
            {
                switch (ext.ToLower())
                {
                    case ".htm":
                    case ".html":
                        type = "text/HTML";
                        break;

                    case ".txt":
                        type = "text/plain";
                        break;

                    case ".doc":
                    case ".rtf":
                    case ".docx":
                        type = "Application/msword";
                        break;
                }
            }
            if (type != "")
                Response.ContentType = type;
            Response.WriteFile(path);
            Response.End();   
            
        }
    }
}
